package com.zy.server.service.impl;

import com.zy.config.RedisKeyConfig;
import com.zy.config.SystemConfig;
import com.zy.dto.UserDto;
import com.zy.server.dao.*;
import com.zy.server.entity.User;
import com.zy.server.entity.Userlog;
import com.zy.server.etype.UserLogType;
import com.zy.server.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.server.util.JwtUtil;
import com.zy.third.JedisUtil;
import com.zy.util.DateUtil;
import com.zy.util.EncryptUtil;
import com.zy.util.StrUtil;
import com.zy.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张鑫
 * @since 2020-11-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserdetailMapper userdetailMapper;

    @Autowired
    private UserlogMapper userlogMapper;

    @Autowired
    private UserscoreMapper userscoreMapper;

    @Autowired
    private UserstatusMapper userstatusMapper;

    // 登录
    @Override
    public R login(UserDto userDto) {
        // 校验参数是否为空
        if (userDto != null) {
            if (StrUtil.checkNoEmpty(userDto.getPhone())) {
                // 唯一登录
                User user = userMapper.selectByPhone(userDto.getPhone());
                if (user != null) {
                    if (user.getFlag() == 1) {
                        //校验账号有没有被冻结
                        //由于本服务用户只能验证码登录，所以不校验密码
                        // 校验当前账号有没有在线
                        if (JedisUtil.getInstance().ishave(RedisKeyConfig.AUTH_PHONE+userDto.getPhone())) {
                            // 存在 则当前账号已在线，生成挤掉信息
                            String token = JedisUtil.getInstance().getStr(RedisKeyConfig.AUTH_PHONE+userDto.getPhone());
                            JedisUtil.getInstance().addHash(RedisKeyConfig.AUTH_OUT, token,"在"+ DateUtil.getTime() + "在其他地方登录！");

                            // 原来的令牌失效
                            JedisUtil.getInstance().delKeys(RedisKeyConfig.AUTH_PHONE+token);
                        }
                        // 生成令牌
                        String token = JwtUtil.createToken(user.getId() + "");
                        // 存储令牌
                        JedisUtil.getInstance().addStrEx(RedisKeyConfig.AUTH_PHONE + user.getPhone(), token, RedisKeyConfig.AUTH_TIME);
                        JedisUtil.getInstance().addStrEx(RedisKeyConfig.AUTH_TOKEN+token,user.getId()+"",RedisKeyConfig.AUTH_TIME);
                        return R.ok(token);
                    }
                }
            }
        }

        return R.fail("亲，登录错误请检查输入哦~");
    }

    @Override
    @Transactional // 开启事务
    public R register(UserDto userDto) {
        //检验参数
        if (userDto != null) {
            if (StrUtil.checkNoEmpty(userDto.getPhone())) {
                // 检验手机号是否可用
                if (userMapper.selectByPhone(userDto.getPhone()) == null) {
                    // 由于没有密码登录，就不构建密文了
                    //String password = EncryptUtil.aesenc(SystemConfig.PASS_KEY, userDto.getPassword());
                    User user = new User();
                    user.setPhone(userDto.getPhone());
                    user.setFlag(1);

                    // 校验是否新增成功
                    if (userMapper.insertId(user) > 0) {
                        // 初始化用户详情数据以及日志
                        userdetailMapper.insertInit(user.getId());
                        userlogMapper.insert(new Userlog(user.getId(), UserLogType.注册.getVal(), "新用户注册，手机号："+userDto.getPhone()));
                        userscoreMapper.insertInit(user.getId(), 0);
                        userstatusMapper.insertInit(user.getId(), 0);
                        return R.ok();
                    }
                }
            }
        }
        return R.fail("亲，注册失败，请检查网络~");
    }

    // 校验手机号是否已经注册
    @Override
    public R checkPhone(String phone) {
        if (StrUtil.checkNoEmpty(phone)) {
            User user = userMapper.selectByPhone(phone);
            if (user == null) {
                return R.ok("未注册");
            }
        }
        return R.fail("已注册");
    }
}
